﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblCust_BranchDAL:Iapp_tblCust_Branch
    {
        HKEA_CMSEntities model;
        app_tblCust_Branch branch;
        private app_tblCust_BranchDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblCust_BranchDAL GetInstance()
        {
            return new app_tblCust_BranchDAL();
        }

        public int InsertENT(app_tblCust_Branch ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblCust_Branch.AddObject(ent);
            if (model.SaveChanges() > 0)
                return model.app_tblCust_Branch.Max(a => a.AppCustBranchUID);
            return 0;
        }
        public List<M_BranchListDetail> GetListByCustUID(int custUID)
        {
            List<M_BranchListDetail> list = new List<M_BranchListDetail>();
            var ret = from app_branch in model.app_tblCust_Branch
                      where
                          app_branch.CustomerUID == custUID && app_branch.Deleted == 0
                      join app_info in model.app_tblContactInfo
                        on app_branch.AppContactInfoUID equals app_info.AppContactInfoUID into infoENT
                      from app_info in infoENT.DefaultIfEmpty()
                      join app_address in model.app_tblAddress
                      on app_branch.AppAddressUID equals app_address.AppAddressUID into addressENT
                      from app_address in addressENT.DefaultIfEmpty()
                      select new M_BranchListDetail
                      {
                          IsFromApp = true,
                          AppCustBranchUID = app_branch.AppCustBranchUID,
                          AddressUID = app_branch.AddressUID ?? 0,
                          CustBranchUID = app_branch.CustBranchUID ?? 0,
                          InfoUID = app_branch.ContactInfoUID ?? 0,
                          AppInfoUID = app_branch.AppContactInfoUID ?? 0,
                          AppAddressUID = app_branch.AppAddressUID ?? 0,
                          BranchName_Chi = app_branch.BranchName_Chi,
                          BranchName_Eng = app_branch.BranchName_Eng,
                          NoOfEmployee = app_branch.NOfEmp ?? 0
                      };
            list = ret.ToList();
            return list;
        }
        public List<app_tblCust_Branch> GetListByENT(app_tblCust_Branch ent, bool IsAddDeleteForSelect)
        {
            var list = model.app_tblCust_Branch.Where(a=>a.AppCustBranchUID > 0);
            if (ent.CustomerUID > 0)
                list = list.Where(a=>a.CustomerUID==ent.CustomerUID);
            if (ent.AppCustBranchUID > 0)
                list = list.Where(a=>a.AppCustBranchUID==ent.AppCustBranchUID);
            if (ent.CustBranchUID > 0)
                list = list.Where(a=>a.CustBranchUID==ent.CustBranchUID);
            if (IsAddDeleteForSelect)
                list = list.Where(a=>a.Deleted==ent.Deleted);
            return list.ToList();
        }

        public int GetCountByENT(app_tblCust_Branch ent)
        {
            throw new NotImplementedException();
        }

        public int UpdateENT(app_tblCust_Branch ent)
        {
            var temp = model.app_tblCust_Branch.Where(a => a.AppCustBranchUID == ent.AppCustBranchUID).FirstOrDefault();
            if (temp != null)
            {
                temp.AddressUID = ent.AddressUID;
                temp.AppAddressUID = ent.AppAddressUID;
                temp.AppContactInfoUID = ent.AppContactInfoUID;
                temp.BranchName_Chi = ent.BranchName_Chi;
                temp.BranchName_Eng = ent.BranchName_Eng;
                temp.ContactInfoUID = ent.ContactInfoUID;
                temp.CustBranchUID = ent.CustBranchUID;
                temp.CustomerUID = ent.CustomerUID;
                temp.Deleted = ent.Deleted;
                temp.IsHQ = ent.IsHQ;
                temp.LastModifyBy = ent.LastModifyBy;
                temp.LastModifyDate = DateTime.Now; 
                temp.NOfEmp = ent.NOfEmp;
                temp.SequenceNo = ent.SequenceNo;
                temp.WebSite = ent.WebSite;
            }
            return model.SaveChanges();
        }

        public List<int> GetCustomerUIDList()
        {
            return model.app_tblCust_Branch.Where(a => a.CustomerUID > 0).Select(a => a.CustomerUID).ToList();
        }

        public int DeleteENT(app_tblCust_Branch ent)
        {
            var temp = model.app_tblCust_Branch.Where(a => a.AppCustBranchUID == ent.AppCustBranchUID).FirstOrDefault();
            if (temp != null)
            {
                model.app_tblCust_Branch.DeleteObject(temp);
            }
            return model.SaveChanges();
        }
    }
}
